// BlkAttriDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "BlkAttriDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBlkAttriDlg dialog


CBlkAttriDlg::CBlkAttriDlg(CWnd* pParent /*=NULL*/)
	: CBCGPDialog(CBlkAttriDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBlkAttriDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_numRep = 0;
	m_repIDs = NULL;
	m_numShowRep = 0;
	m_showRepIDs = NULL;
	m_sAttName = NULL;
	m_strTitle = _T("");
}

CBlkAttriDlg::~CBlkAttriDlg()
{
	if (m_repIDs != NULL)
	{
		delete []m_repIDs;
		m_repIDs = NULL;
	}
	if (m_showRepIDs != NULL)
	{
		delete []m_showRepIDs;
		m_showRepIDs = NULL;
	}
	if (m_sAttName !=NULL)
	{
		delete []m_sAttName;
		m_sAttName = NULL;
	}
}
void CBlkAttriDlg::DoDataExchange(CDataExchange* pDX)
{
	CBCGPDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBlkAttriDlg)
	DDX_Control(pDX, IDC_PROPLIST_LOCATION, m_wndPropListLocation);
// 	DDX_Control(pDX, IDC_CB_BLK_ATTRI, m_cbBlkAttri);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBlkAttriDlg, CBCGPDialog)
	//{{AFX_MSG_MAP(CBlkAttriDlg)
	ON_WM_MOUSEACTIVATE()
	ON_BN_CLICKED(IDC_BLK_ATTRE_SELALL, OnBlkAttreSelall)
	ON_BN_CLICKED(IDC_BLK_ATTRE_SELNONE, OnBlkAttreSelnone)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBlkAttriDlg message handlers

BOOL CBlkAttriDlg::OnInitDialog() 
{
	CBCGPDialog::OnInitDialog();

	EnableVisualManagerStyle(TRUE,TRUE);
	SetWindowText(m_strTitle);
	
	CRect rectProplist;
	m_wndPropListLocation.GetClientRect(&rectProplist);
	m_wndPropListLocation.MapWindowPoints(this, &rectProplist);
	m_wndGrid.Create (WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, rectProplist, this, IDC_BLK_PROPLIST);
	m_wndGrid.SetSingleSel (FALSE);
	m_wndGrid.EnableMarkSortedColumn (FALSE);
	m_wndGrid.EnableHeader(TRUE, ~BCGP_GRID_HEADER_SORT);
	m_wndGrid.SetRowMarker(FALSE);
	m_wndGrid.SetReadOnly(FALSE);
	m_wndGrid.SetEditFirstClick (FALSE);
	m_wndGrid.SetGridHeaderHeight(10);
	m_wndGrid.SetShowInPlaceToolTip(FALSE);
	m_wndGrid.EnableDragHeaderItems (FALSE);
	m_wndGrid.SetColumnAlign(0, HDF_CENTER);
	m_wndGrid.SetHeaderAlign(0, HDF_CENTER);
	m_wndGrid.SetHeaderAlign(1, HDF_CENTER);

   AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CString strName;
	strName.LoadString(IDS_STRING_VALUE);
	m_wndGrid.InsertColumn (0, (LPCTSTR)strName, 80);
	strName.LoadString(IDS_STRING_SEL);
	m_wndGrid.InsertColumn (1,(LPCTSTR)strName, 40);

	
	for (int i=0; i<m_numRep; i++)
	{
		m_wndGrid.AddRow(FALSE);
		CBCGPGridRow *pRow = m_wndGrid.GetRow(i);
		CString str;
		bool bCheck = FALSE;
// 		str.Format("%d", m_repIDs[i]);
// 		pRow->GetItem(0)->SetValue(_variant_t(str));
		pRow->GetItem(0)->SetValue(m_sAttName[i].attName);
		pRow->GetItem(0)->AllowEdit(FALSE);
		for (int j=0; j<m_numShowRep; j++)
		{
			if (m_repIDs[i] == m_showRepIDs[j])
			{
				bCheck = TRUE;
				break;
			}	
		}
		pRow->ReplaceItem(1, new CBCGPGridCheckItem(bCheck));
	}
	m_wndGrid.AdjustLayout();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CBlkAttriDlg::OnOK()
{
	CBCGPDialog::OnOK();
	int numRep=0;
	for (int i=0; i<m_numRep; i++)
	{	
		bool bShow = false;
		bShow = (bool)(m_wndGrid.GetRow(i)->GetItem(1)->GetValue());
		if (bShow)
		{
// 			int attvalue = (long)(m_wndGrid.GetRow(i)->GetItem(0)->GetValue());
			m_showRepIDs[numRep] = i+1;
			numRep++;
		}
	}
	m_numShowRep = numRep;
}

void CBlkAttriDlg::OnBlkAttreSelall() 
{
	for (int i=0; i<m_numRep; i++)
	{	
		m_wndGrid.GetRow(i)->GetItem(1)->SetValue(_variant_t(true));
	}
}

void CBlkAttriDlg::OnBlkAttreSelnone() 
{
	for (int i=0; i<m_numRep; i++)
	{	
		m_wndGrid.GetRow(i)->GetItem(1)->SetValue(_variant_t(false));
	}
}

void CBlkAttriDlg::SetAllAttributes(int *att, int num)
{
	if (m_repIDs != NULL)
	{
		delete []m_repIDs;
		m_repIDs = NULL;
	}
	m_repIDs = new int[num];
	for (int i=0; i<num; i++)
	{
		m_repIDs[i] = att[i];
	}
	m_numRep = num;
}
void CBlkAttriDlg::SetShowAttributes(int *att, int num)
{
	if (m_showRepIDs != NULL)
	{
		delete []m_showRepIDs;
		m_showRepIDs = NULL;
	}
	m_showRepIDs = new int[m_numRep];
	for (int i=0; i<num; i++)
	{
		m_showRepIDs[i] = att[i];
	}
	m_numShowRep = num;
}

void CBlkAttriDlg::setAttributeNames(BLOCK_ATTNAME *sAttName, int num)
{
	if (m_sAttName != NULL)
	{
		delete []m_sAttName;
		m_sAttName = NULL;
	}
	m_sAttName = new BLOCK_ATTNAME[m_numRep];
	for (int i=0; i<num; i++)
	{
		m_sAttName[i] = sAttName[i];
	}
	m_numRep = num;

}